// pages/home/payment/payment.js
import {fetchData,resultJudge} from '../../../utils/request'
import {getUserInfo} from '../../../utils/util'
import Dialog from '../../../miniprogram_npm/vant-weapp/dialog/dialog';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    checked: false,
    isAgree:false,
    activeIcon: 'http://39.107.110.103:9000/property-management/images/pay/radio-active.png',
    inactiveIcon: 'http://39.107.110.103:9000/property-management/images/pay/radio.png',
    active: 0,
    userInfo:{},
    PaymentList:[],
    totalAmount:0,
    show:false,
    isEmpty:false,
    PaymentLogs:[],
    isEmptyLog:true,
    contactList:[]
  },
  //================用户自定义方法=========
  showDialog(){
   this.setData({
       show:true
   })
  },
  //================获取缴费记录=============
  getPaymentLogs(){
      let userId=this.data.userInfo.userId
      fetchData(`/base/log/listLog?userId=${userId}`,"","GET",res=>{
      let PaymentLogs= resultJudge(res,"list");
      if(PaymentLogs.length>0){
        this.setData({
            PaymentLogs:PaymentLogs,
            isEmptyLog:false
        })
      }
      
      })
  },
  //=================计算总价格==============
  computeAmount(){
    let id= this.data.userInfo.userId;
     fetchData("/base/payment/getPaymentTotalAmount/"+id,"","GET",res=>{
         if(res.data.code==200){
            this.setData({
                totalAmount:res.data.data
            })
         }
     },err=>{

     })

     
  },
  getContactList(){
      fetchData("/base/contact/list",{isban:0},"GET",res=>{
      let result= resultJudge(res,"list")
      this.setData({
        contactList:result
      })
      })
  },
  contactMe(e){
      let phone=e.currentTarget.dataset.phone
    wx.makePhoneCall({
        phoneNumber: phone, //仅为示例，并非真实的电话号码
        success() {
          console.log('接口调用成功的回调函数')
        },
        fail() {
          console.log('接口调用失败的回调函数')
        },
        complete() {
          console.log('接口调用结束的回调函数（调用成功、失败都会执行）')
        }
      })
  
  },
  getPaymentList(){
      if(!this.data.userInfo.userId){
      return;
      }
      let payment={
          orderStatus:-1,
          payUserId:this.data.userInfo.userId
      }
      fetchData("/base/payment/getPaymentList",payment,"POST",res=>{
        if(res.data.code==200){
            if(res.data.rows.length==0){
                this.setData({
                    isEmpty:true
                })
            }
       this.setData({
           PaymentList:res.data.rows
       })
       this.computeAmount()
        }
      },err=>{

      })
  },
  getUserInfo(){
    let user=getUserInfo();
    this.setData({
        userInfo:user
    })
  },
  onChange(event) {
    this.setData({
      checked: event.detail,
    });
  },
  onChangeAgree(event) {
    this.setData({
        isAgree: event.detail,
        show:true
    });
  },
  paySubmit(){
      if(!this.data.checked){
          wx.showToast({
            title: '请选中缴费项目',
            icon:"error"
          })
          return
      }
      if(!this.data.isAgree){
        wx.showToast({
            title: '请先同意说明',
            icon:"error"
          })
          return
      }
     let ids= this.data.PaymentList.map(item=>{
          return item.id
      })
      ids=ids.join(',')
     wx.navigateTo({
       url: `/pages/home/payment/pay/pay?ids=${ids}&totalAmount=${this.data.totalAmount}&payUserId=${this.data.userInfo.userId}`,
     })
  },
  toPaymentDetail(e){
    let id=  e.currentTarget.dataset.id
    if(id){
        wx.navigateTo({
          url: './detail/detail?id='+id
        })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
  this.getUserInfo()//获取用户信息
  this.getContactList() //获取物业联系人列表
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getPaymentList() //获取缴费信息
    this.getPaymentLogs() //获取缴费记录
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})